package com.zhongyi.search;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

public class TestSearch {
	public static void main(String[] args) {
		 HttpClient httpclient = new DefaultHttpClient();
	        try {
	            HttpGet httpget = new HttpGet("http://www.ecplaza.net/trade-leads-buyer/required-a-regular-supplier-for--4747810.html");

	            System.out.println("executing request " + httpget.getURI());

	            // Create a response handler
	            ResponseHandler<String> responseHandler = new BasicResponseHandler();
	            String responseBody = httpclient.execute(httpget, responseHandler);
	            String matchString = parseHtml(responseBody);
//	            System.out.println("----------------------------------------");
	            System.out.println(responseBody);
//	            System.out.println("----------------------------------------");

	        } catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
	            // When HttpClient instance is no longer needed,
	            // shut down the connection manager to ensure
	            // immediate deallocation of all system resources
	            httpclient.getConnectionManager().shutdown();
	        }
	}
	
	public static String getMatchStringFromHtml(String htmlString,String[] regexs){
		StringBuilder back = new StringBuilder("");
		if(regexs != null){
			Pattern p = null;
			Matcher matcher = null;
			for(String regex : regexs){
				p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
				matcher = p.matcher(htmlString);
				if(matcher.find()){
					back.append(matcher.group());
				}
			}
		}
		return back.toString();
	}
	
	public static String parseHtml(String htmlString){
		 Parser parser =  null;
		 TableTag tableTag = null;
		 NodeList nodeList = null;
		 NodeList tdNodes = null;
		 Node trNode = null;
		 Node thNode = null;
		 String thText = null;
		 SimpleNodeIterator nodeIter = null;
		 //CSS样式匹配
		 NodeFilter filter = new HasAttributeFilter("class", "contact_info");
		
		 try {
			parser = new Parser(htmlString);
//			parser.setURL("http://www.ecplaza.net/trade-leads-buyer/required-a-regular-supplier-for--4747810.html");
			nodeList = parser.extractAllNodesThatMatch(filter);
			tableTag = (TableTag) nodeList.elementAt(0);
			nodeIter = tableTag.children();
			while(nodeIter.hasMoreNodes()){
				//TR节点
				trNode = nodeIter.nextNode();
				//TH子节点
				tdNodes = trNode.getChildren().extractAllNodesThatMatch(new TagNameFilter("th"));
				thNode = tdNodes.elementAt(0);
				thText = thNode.getText();
				//Company
				if(thText != null && thText.indexOf("Company:") != -1){
					System.out.println("Company:" + thNode.getNextSibling().getText());
				}
				if(thText != null && thText.indexOf("Country/Region:") != -1){
					System.out.println("Country/Region:" + thNode.getNextSibling().getText());
				}
				if(thText != null && thText.indexOf("Address:") != -1){
					System.out.println("Address:" + thNode.getNextSibling().getText());
				}
				if(thText != null && thText.indexOf("Contact:") != -1){
					System.out.println("Contact:" + thNode.getNextSibling().getText());
				}
				if(thText != null && thText.indexOf("Phone:") != -1){
					System.out.println("Phone:" + thNode.getNextSibling().getText());
				}
				if(thText != null && thText.indexOf("Fax:") != -1){
					System.out.println("Fax:" + thNode.getNextSibling().getText());
				}
				if(thText != null && thText.indexOf("Mobile:") != -1){
					System.out.println("Mobile:" + thNode.getNextSibling().getText());
				}
				if(thText != null && thText.indexOf("Tags:") != -1){
					System.out.println("Tags:" + thNode.getNextSibling().getText());
				}
			}
		} catch (ParserException e) {
			e.printStackTrace();
		}
		return null;
	}
}
